<?php
class Macys_Api_V2_User2 {

	protected $log;
	protected static $m_pInstance;
	protected  $userEndPoint = "/v2/user";
	protected $userUri;

	protected $proxyUri;
	protected $directUri;
	protected $client_id;
	protected $client;
	protected $_config;

	protected $userId;
	
	private $showArray = array();

	private function __construct() {
		$this->log = Zend_Registry::get('log');
		$this->_config = Macys_Base_Config::getInstance();
		$this->setInputConf();
		$this->client = new Macys_Base_HttpClient();
		$this->client->setCurlSupport();
	}

	public function getUserDetails() {
		$this->setUri();
		$this->setUserAccess();
		$this->setDefaultHeaders();
		$this->setShowHeaders();
		$this->client->request('GET');
		return $this->client->getResponseBodyArray();
	}

	// Used by Shopping bag & Others
	public function getUserId() {
		return $this->userId;
	}
	
	public function setParams($params) {
		$this->setShowParam('shipping');
		$this->setShowParam('summary');
	}

	private function setShowHeaders() {
		$v = implode(',', $this->showArray);
		$this->client->setParameterGet('show',$v);
	}

	private function setShowParam($showValue) {
		if(in_array($showValue, array('shipping','summary'))) {
			if(!in_array($showValue, $this->showArray)) {
				$this->showArray[] = $showValue;
			}
			$this->log->debug("show Param already set : " . $showValue);
		}
		else {
			$this->log->debug('Show Params Value is not Approriate : Value tried to set is : ' . $showValue);
		}
	}


	protected function setDefaultHeaders() {
		$this->client->setAcceptType();
		$this->client->setContentTypeJson();
	}

	protected function setUserAccess() {
		$x = $this->_config->getValues('UserContext');
		if(isset($x) && !empty($x)) {
			$this->client->setUserContextHeader($this->_config['UserContext']);
			$this->client->setClientHeader('dsdf');
		} else {
			$this->client->setUserAccessHeader($this->getAccessToken());
			$this->client->setClientHeader($this->client_id);
		}

	}

	protected function setUri() {
		$this->userUri = $this->proxyUri . $this->userEndPoint;
		$this->client->setUri($this->userUri);
	}


	/**
	 * Singleton instance of the User
	 * as User data is not shared one instance per user
	 */
	public static function getInstance() {
		if (!self::$m_pInstance){
			self::$m_pInstance = new self();
		}
		return self::$m_pInstance;
	}


	public function isAuthenticated() {
		return Zend_Auth::getInstance()->hasIdentity();
	}

	private function getAccessToken() {
		if($this->isAuthenticated()) {
			return Zend_Auth::getInstance()->getIdentity();
		}
		else {
			return null;
			throw new Macys_Api_V2_Exception('Authentication information is not available ');
		}
	}


	/**
	 * Setup the Config Data
	 */
	private function setInputConf() {
		$conf = $this->_config->getOAuthDetails();
		$this->proxyUri = $conf['mashery_uri'];
		$this->directUri = $conf['auth_uri'];
		$this->client_id = $conf['client_id'];
	}
}